package com.longrise.android.byjk.plugins.login;

import android.app.Activity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.R;
import com.longrise.android.byjk.common.Control;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.common.datasource.local.sp.CacheUtils;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.DZZWTools;
import com.longrise.android.byjk.widget.view.InputBoxView;

/**
 * 重置密码
 * <p/>
 * <p/>
 * detailed comment
 *
 * @author call 2015-12-31 * @see
 * @since 1.0
 */
public class ResetPasswordActivity extends Activity implements OnClickListener {

    private Button mBt;

    private RelativeLayout mRlback;

    private TextView mTvTitle;

    private TextView mTvyhzh;

    private InputBoxView mBvpassword;

    private InputBoxView mBvpassword_cf;
    private String mUsername;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_resetpassword);
        initView();
        initData();
        regEvent(true);
    }

    @Override
    protected void onStart() {
        super.onStart();
    }

    private void initView() {
        mBt = (Button) findViewById(R.id.resetpassword_bt);
        mRlback = (RelativeLayout) findViewById(R.id.relapp_back);
        mTvTitle = (TextView) findViewById(R.id.app_title);
        mTvyhzh = (TextView) findViewById(R.id.resetpassword_tv_yhzh);
        mBvpassword = (InputBoxView) findViewById(R.id.resetpassword_newpw);
        mBvpassword_cf = (InputBoxView) findViewById(R.id.resetpassword_cfpw);
    }

    private void initData() {
        mTvTitle.setText("重置密码");
        mUsername = UserInfor.getInstance().getWjmmzh();
        mTvyhzh.setText(mUsername);

        mBvpassword.setRightClickMode(InputBoxView.PASSWORD);
        mBvpassword.setTextMode(InputBoxView.PASSWORD);
        mBvpassword_cf.setRightClickMode(InputBoxView.PASSWORD);
        mBvpassword_cf.setTextMode(InputBoxView.PASSWORD);
    }

    private void regEvent(boolean b) {
        if (mBt != null) {
            mBt.setOnClickListener(b ? this : null);
        }

        if (mRlback != null) {
            mRlback.setOnClickListener(b ? this : null);
        }

    }

    /**
     * @see android.view.View.OnClickListener#onClick(android.view.View)
     */
    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.resetpassword_bt: // 重置密码
                /*
                 * View view = View.inflate(this, R.layout.dialog_reenter,
                 * null); DialogUtil.getInstance().creatAlertDialog(this, view,
                 * 266, 126);
                 */
                toResetPassword();

                break;
            case R.id.relapp_back:
                finish();
                break;
            default:
                break;
        }
    }

    /**
     * 重置密码
     */
    private void toResetPassword() {
        String password = mBvpassword.getText();
        if (TextUtils.isEmpty(password)) {
            DZZWTools.showToast(ResetPasswordActivity.this, "请输入新密码", 0);
            return;
        }
        if (!password.matches("[0-9a-z-A-Z_]{6,18}")) {
            DZZWTools.showToast(ResetPasswordActivity.this, "密码只能包含字母、数字和下划线，6-18位", 0);
            return;
        }

        String password2 = mBvpassword_cf.getText();
        if (TextUtils.isEmpty(password2)) {
            DZZWTools.showToast(ResetPasswordActivity.this, "请输入确认密码", 0);
            return;
        }

        if (!password.equals(password2)) {
            DZZWTools.showToast(ResetPasswordActivity.this, "两次输入密码不一致", 0);
            return;
        }
        EntityBean bean = new EntityBean();
        bean.set("username", mUsername);
        bean.set("newpwd", password);
        EntityBean fbean = new EntityBean();
        fbean.set("bean", bean);

        DZZWTools.showWaitDialog(ResetPasswordActivity.this);
        mBt.setClickable(false);
        LoadDataManager.getInstance().callService("", UrlConstants.BaseUrl, "bbt_login_uChangePWD",
                fbean, new LoadDataManager.OnRequestCallListener() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    DZZWTools.dismissDialog(null);
                    EntityBean bean = (EntityBean) result;
                    String resultdesc = (String) bean.get("resultdesc");
                    int resultstate = (Integer) bean.get("resultstate");
                    if (resultstate == 1) //成功
                    {
                        LoadDataManager.getInstance().setReloginBean(null);
                        DZZWTools.showToast(ResetPasswordActivity.this, "重置密码成功,请重新登录", 0);
                        //将用户名保存本地 方便下次进入软件时 自动填写
                        CacheUtils.setString(Control.USERNAME, mUsername);
                        finish();
                    } else if (resultstate == 4) {
                        DZZWTools.showToast(ResetPasswordActivity.this, "新密码与原密码相同", 0);
                    } else if (resultstate == 0) {
                        DZZWTools.showToast(ResetPasswordActivity.this, "操作失败", 0);
                    } else if (resultstate == 2) {
                        DZZWTools.showToast(ResetPasswordActivity.this, "账户不存在", 0);
                    }



                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            @Override
            public void onFinished(String key, String service) {
                DZZWTools.dismissDialog(null);
                mBt.setClickable(true);
            }

            @Override
            public void onError(String key, String service, Throwable ex, boolean isOnCallback) {
                DZZWTools.showToast(ResetPasswordActivity.this, "操作失败", 0);
            }
        });
    }

    /**
     * @see android.app.Activity#onDestroy()
     */
    @Override
    protected void onDestroy() {
        super.onDestroy();

        regEvent(false);
        mBt = null;

        mRlback = null;

        mTvTitle = null;

        mTvyhzh = null;

        mBvpassword = null;

        mBvpassword_cf = null;

    }

}
